
package com.scalar.chiptrack.users.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.Location;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.users.dao.AdaptorSetUpDAO;
import com.scalar.chiptrack.users.forms.AdapterSettingForm;
//import com.scalar.chiptrack.jobs.Job;
//import com.scalar.chiptrack.jobs.dao.JobDAO;

public final class AdapterSettingAction extends ChipTrackAction {


    // ----------------------------------------------------- Instance Variables
           

    // --------------------------------------------------------- Public Methods

    public ActionForward execute(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	throws Exception {

		// Extract attributes and parameters we will need
		AdapterSettingForm subform = (AdapterSettingForm) form;
		String action = request.getParameter("action");
		System.out.println("In the action" + action);
		// Validate the request parameters specified by the user
		

		if(action.equals("vendor") || action.equals("save")){
		request.setAttribute("vendorList",loadVendorAdaptors(AdaptorSetUpDAO.getVendorAdaptor().iterator()));
		request.setAttribute("action",action);
		int locationId = subform.getLocationId();
	    
		   if(action.equals("vendor")){
		   subform = AdaptorSetUpDAO.getVendorAdaptorInfo(locationId);
		   subform.setLocationId(locationId);
		   request.setAttribute("adapterSettingForm",subform);
		   return (mapping.findForward("success"));
		   }
			
		   if(action.equals("save")){
			   HashMap errors = new HashMap();
				HashMap info = new HashMap();

                request.setAttribute (SessionKeys.ERROR_MESSAGES, errors);
                request.setAttribute (SessionKeys.INFO_MESSAGES, info);
			    
				if(locationId == 0){	
				request.setAttribute("adapterSettingForm",subform);
				//request.setAttribute("ErrMsg","Please Select Appropriate Vendor first");				
				return (mapping.findForward("success"));


				}


				DBTransaction dbTransaction = null; 
				boolean success =false;
				try{
						String same = subform.getSame();
                                                
						//String jobId ="";
						//Job job = null;
						
                                                if(same==null)
						same="";
						dbTransaction = DBTransaction.getInstance();
						dbTransaction.begin();
						if(same.equals("y")){
							
                                                    /* --- SKIP UPDATING JOB INFORMATION --- Balaram on Nov 21, 2002 *\
                                                     
                                                         ArrayList jobList = AdaptorSetUpDAO.getJobId(dbTransaction,subform.getVendorName());
							for(int i=0;i<jobList.size();i++){
								job = new Job();
								jobId =(String) jobList.get(i);
								job.setId(Integer.parseInt(jobId));
								job.setName(subform.getJobName());
								job.setHandler(subform.getJobHandler());
								if(subform.getFrequency()!=null)
								job.setFrequency(Long.parseLong(subform.getFrequency()) * 60);
								JobDAO.updateJob(dbTransaction,job);
                                                        }
                                                    \* --- ----  ---- ---- ---- --- ----  --- ---- ---- --- --- --- */
                                                        
                                                    AdaptorSetUpDAO.saveSameVendorAdaptorInfo(dbTransaction,subform);
						}
						else{   
                                                    
                                                    /* --- SKIP UPDATING JOB INFORMATION --- Balaram on Nov 21, 2002 *\

                                                                job = new Job();
								job.setId(subform.getJobId());
								job.setName(subform.getJobName());
								job.setHandler(subform.getJobHandler());
								if(subform.getFrequency()!=null)
								job.setFrequency(Long.parseLong(subform.getFrequency()) * 60);
								JobDAO.updateJob(dbTransaction,job);

                                                   \* --- ----  ---- ---- ---- --- ----  --- ---- ---- --- --- --- */
                                                    
								AdaptorSetUpDAO.saveVendorAdaptorInfo(dbTransaction,subform);
						     }

						request.setAttribute("adapterSettingForm",subform);
						info.put("Data Saved Successfully",null);
						//request.setAttribute("ErrMsg","Data Saved Successfully");
						success = true;
				   }
				
					catch (Throwable t){
                    //request.setAttribute("ErrMsg","Problem in Saving....");
					errors.put("Internal Server Error","Exception while saving the Vendor");
					t.printStackTrace();
					}
					finally {
						// Rollback or Commit the Transaction
							if (success == true) {
							dbTransaction.commit();
							}
							else {
							dbTransaction.rollback();
							}
					}

		   }
						return (mapping.findForward("success"));

		   }

					return (mapping.findForward("success"));
		
		   }
		      
                		
		   
   private static ArrayList loadVendorAdaptors(Iterator locs) throws ScalarException
   {
        ArrayList vendorList = new ArrayList();
        
        //Iterator locs = vendorLocations.iterator();
        while(locs.hasNext()) {
            Location location = (Location) locs.next();
            //System.out.println("Location Id"+ location.getLocationName() + "  "+ location.getLocationID());
            vendorList.add(new org.apache.struts.util.LabelValueBean(
                                 location.getVendorName(),new Integer(location.getLocationId()).toString()));
        }
        
        return vendorList;
   }
   
   
     
   
}
